package com.ebt.m.homepage;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.widget.PopupWindowCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ebt.m.R;

/**
 * Created by Andy.Wang on 2016/2/22.
 */
public class SpinnerMenu extends PopupWindow {
    private View mAnchor;
    private Context mContext;
    private String[] itemStrs;
    private TypeAdapter listAdapter;
    private int mCurrentSelectedPosition;
    private int mItemDefaultColor;
    private int mItemSelectedColor;

    private View mContentView;
    private ListView mContentList;
    private OnTypeSelectedListener onTypeSelectedListener;
    private static int offset = 60;

    public SpinnerMenu(Context context, View anchor, String[] array, int initSelected) {
        super(null, anchor.getWidth() + offset, ViewGroup.LayoutParams.WRAP_CONTENT);
        this.mContext = context;
        this.mAnchor = anchor;
        initContentView(array, initSelected);

    }

    public void popShow(int initSelected) {
        mCurrentSelectedPosition = initSelected;
        PopupWindowCompat.showAsDropDown(this,mAnchor,0,0,Gravity.BOTTOM|Gravity.RIGHT);

    }

    public void popShow() {
        PopupWindowCompat.showAsDropDown(this,mAnchor,0,0,Gravity.BOTTOM|Gravity.RIGHT);
    }


    private void initContentView(String[] array, int initSelected) {
        mItemDefaultColor = mContext.getResources().getColor(R.color.spinner_item_bg);
        mItemSelectedColor = mContext.getResources().getColor(R.color.spinner_item_bg_selected);
        mCurrentSelectedPosition = initSelected;
        mContentView = View.inflate(mContext, R.layout.layout_4message_pop, null);
        mContentList = (ListView) mContentView.findViewById(R.id.content_view);
        itemStrs = array;
        listAdapter = new TypeAdapter();
        mContentList.setAdapter(listAdapter);
        // 点击listview中item的处理
        mContentList
                .setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0,
                                            View arg1, int arg2, long arg3) {

                        mCurrentSelectedPosition = arg2;
                        if (onTypeSelectedListener != null) {
                            onTypeSelectedListener.onTypeSelected(arg2, (String) listAdapter.getItem(arg2));
                        }
                    }
                });

        ColorDrawable cd = new ColorDrawable(Color.parseColor("#00bbbbbb"));
        setBackgroundDrawable(cd);
        setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        setTouchable(true); // 设置popupwindow可点击
        setOutsideTouchable(true); // 设置popupwindow外部可点击
        setFocusable(true); // 获取焦点

        setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // 如果点击了popupwindow的外部，popupwindow也会消失
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    dismiss();
                    return true;
                }
                return false;
            }
        });
        setContentView(mContentView);
    }

    private class TypeAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return itemStrs.length;
        }

        @Override
        public Object getItem(int position) {
            return itemStrs[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.layout_4message_pop_item, parent, false);
            TextView typeLable = (TextView) view.findViewById(R.id.type_label);
            ImageView imageView = (ImageView) view.findViewById(R.id.img_checked);
            imageView.setVisibility(position == mCurrentSelectedPosition ? View.VISIBLE : View.INVISIBLE);
            typeLable.setText((String) getItem(position));
            return view;
        }
    }

    public void setOnTypeSelectedListener(OnTypeSelectedListener listener) {
        this.onTypeSelectedListener = listener;
    }

    public interface OnTypeSelectedListener {
        void onTypeSelected(int position, String title);
    }

}
